package kr.pe.johnkim.pushchat.mobile.controller;

import kr.pe.johnkim.pushchat.mobile.domain.PushChatFriend;
import kr.pe.johnkim.pushchat.mobile.service.PushChatFriendService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : John_Kim <cale9797@gmail.com>
 * @date : 13. 6. 26.
 */
@Controller
@RequestMapping(value = "/pushChatFriendController")
public class PushChatFriendController {

    // SLF4J Logging
    private Logger logger = LoggerFactory.getLogger(PushChatFriendController.class);

    @Autowired
    private PushChatFriendService pushChatFriendService;

    /**
     * 친구 중복 확인을 한다.
     *
     * @param pushChatFriend
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/selectDuplicationFriend", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public PushChatFriend selectDuplicationFriend(@RequestBody PushChatFriend pushChatFriend) throws Exception {
        logger.info("selectDuplicationFriend");
        return this.pushChatFriendService.selectDuplicationFriend(pushChatFriend);
    }

    /**
     * 친구를 등록 한다.
     *
     * @param pushChatFriend
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/registerFriend", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public PushChatFriend registerFriend(@RequestBody PushChatFriend pushChatFriend) throws Exception {
        logger.info("registerFriend");

        PushChatFriend result = new PushChatFriend();

        try {
            this.pushChatFriendService.registerFriend(pushChatFriend);
            result.setResult("SUCCESS");
            logger.info("success to register friend.");
        } catch (Exception e) {
            result.setResult("FAIL");
            logger.error("fail to register friend.");
        }

        return result;
    }

    /**
     * 친구를 비활성화 한다.
     *
     * @param pushChatFriend
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/updateDeactivateFriend", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public PushChatFriend updateDeactivateFriend(@RequestBody PushChatFriend pushChatFriend) throws Exception {
        logger.info("updateDeactivateFriend");

        PushChatFriend result = new PushChatFriend();

        try {
            this.pushChatFriendService.updateDeactivateFriend(pushChatFriend);
            result.setResult("SUCCESS");
            logger.info("success to update deactivate.");
        } catch (Exception e) {
            result.setResult("FAIL");
            logger.info("fail to update deactivate.");
        }

        return result;
    }

    /**
     * 친구 목록을 가져 온다.
     *
     * @param pushChatFriend
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/selectFriendList", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public List<PushChatFriend> selectFriendList(@RequestBody PushChatFriend pushChatFriend) throws Exception {
        logger.info("selectFriendList");
        return this.pushChatFriendService.selectFriendList(pushChatFriend);
    }
}
